package com.fx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fx.util.OSSClientUtil;
import com.fx.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: EDZ
 * @Date: 2018-5-18 17:50:
 * @Email: 你的邮箱
 * 概要说明：该类干嘛用的
 **/
@Controller
@RequestMapping("/commons")
public class BaseController {

    @Autowired
    private OSSClientUtil ossClientUtil;

    public JSONObject renderPage(int count, Object data) {
        JSONObject uiData = new JSONObject();
        uiData.put("success", true);
        uiData.put("rows", data);
        uiData.put("message", "加载成功");
        uiData.put("total", count);
        return uiData;
    }


    @RequestMapping("/upload")
    @ResponseBody
    public R fileUpload(@RequestParam("file") CommonsMultipartFile file, String url) {
        if (url != null && !"".equals(url)) {
            String first = url.substring(url.indexOf("/") + 2);
            String sec = first.substring(first.indexOf("/") + 1);
            String groupName = sec.substring(0, sec.indexOf("/"));
            String fileName = sec.substring(groupName.length() + 1);
        }
        String name = ossClientUtil.uploadImg2Oss(file);
        System.out.println(name);
        String tempUrl = ossClientUtil.getUrl(name);
        System.out.println(tempUrl.split("\\?")[0]);
        String realUrl = tempUrl.split("\\?")[0];
        Map<String, String> res = new HashMap<String, String>();
        res.put("url", realUrl);
        return R.ok(res);
    }
}
